<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/12
 * Time: 11:21
 */

namespace app\api\logic;


use app\api\model\DeviceStatusRecordModel;
use app\api\model\DistributorModel;
use app\api\model\EquipModel;
use app\api\model\FaceRecognitionModel;
use app\api\model\FaceStaffModel;
use app\api\model\ShopClerksModel;
use app\api\model\StoreinLogModel;
use app\api\model\StoreoutLogModel;

class FaceRecognitionLogic
{
    /**
     * 保存人脸识别数据
     * @param $data
     * @return int|string
     */
    public function getFaceData($data)
    {
        $equip = new DistributorModel();
        $isCome = $equip->isCome($data['device_id'], $data['channel_id']);
        $shopId = $equip->getShopIdByEquipInfo($data['device_id'], $data['channel_id']);
        $saveData = [
            'photo' => $data['face_image_url'] ?? '',
            'face_id' => $data['face_id'],
            'sex' => $this->genderToNum($data['gender']),
            'age' => $data['age'] ?? '',
            'enter_time' => time(),
            'equipment_id' => $data['device_id'],
            'channel_id' => $data['channel_id'],
            'is_glasses' => $data['glasses'] ?? '',
            'is_hat' => $data['hat'] ?? '',
            'hair' => $data['hair'] ?? '',
            'mouth_mask' => $data['mouth_mask'] ?? '',
            'face_expression' => $data['face_expression'] ?? '',
            'global_obj_id' => $data['global_obj_id'] ?? '',
            'report_time' => $data['report_time'] ?? '',
            'create_time' => time(),
            'shop_id' => $shopId
        ];
        try {
            $result = FaceRecognitionModel::insert($saveData);
            if ($isCome == 2) {
                StoreoutLogModel::insert($saveData);
            } else {
                StoreinLogModel::insert($saveData);
            }
        } catch (\Exception $e) {
            return false;
        }
        if ($result || $shopId != 0) {
            $shopClerksId = (new ShopClerksModel())->getAShopClerksId($data['face_id']);
            p($shopClerksId);
            if ($shopClerksId != 0) {
                $saveData['shop_clerks_id'] = $shopClerksId;
                return FaceStaffModel::insert($saveData);
            }
            return true;
        }
        return false;
    }

    /**
     * 保存设备异常信息
     * 文档在doc文件夹
     * @param $data
     * @return bool|int|string
     */
    public function getDeviceStatus($data)
    {
        $saveData = [
            'device_id' => $data['device_id'] ?? '',
            'device_name' => $data['device_name'] ?? '',
            'time' => $data['time'] ?? '',
            'behavior' => $data['method']
        ];
        if ($data['method'] == 'logout') {
            $saveData['online_time'] = $data['online_time'];
            $saveData['last_login_time'] = strtotime($data['last_login_time']);
            $result = DeviceStatusRecordModel::insert($saveData);
            //将设备改为下线状态
            EquipModel::where('equip_id', $saveData['device_id'])->update(['status' => 2, 'update_time' => time()]);
        } else {
            $channelList = $data['video_channel'] ?? [];
            if (count($channelList) <= 0) {
                return false;
            }
            $insertData = [];
            foreach ($channelList as $key => $value) {
                $insertData[] = [
                    'device_id' => $saveData['device_id'],
                    'device_name' => $saveData['device_name'],
                    'time' => $saveData['time'],
                    'behavior' => $saveData['behavior'],
                    'channel_id' => $value['channel_id'],
                    'channel_name' => $value['channel_name'],
                    'channel_state' => $value['channel_state'],
                    'channel_ability' => json_encode($value['channel_ability'] ?? []),
                    'device_type' => $value['device_type'],
                    'manufacture' => $value['manufacture'],
                    'firmware' => $value['firmware'],
                    'model' => $value['model'],
                ];
            }
            $result = DeviceStatusRecordModel::insertAll($insertData);
            foreach ($insertData as $k => $vo) {
                //设备登录
                if ($vo['behavior'] == 'login') {
                    EquipModel::where('equip_id', $vo['device_id'])->where('channel_id', $vo['channel_id'])
                        ->update(['status' => 1, 'update_time' => time()]);
                }
                //通道离线/上线
                if ($vo['behavior'] == 'channel_state_notice') {
                    //channel_state=0下线,1上线
                    if ($vo['channel_state'] == 1) {
                        EquipModel::where('equip_id', $vo['device_id'])->where('channel_id', $vo['channel_id'])
                            ->update(['status' => 1, 'update_time' => time()]);
                    } else{
                        EquipModel::where('equip_id', $vo['device_id'])->where('channel_id', $vo['channel_id'])
                            ->update(['status' => 2, 'update_time' => time()]);
                    }
                }
            }
        }
        return $result;
    }

    /**
     * 性别转数字
     * @param $gender
     * @return int
     */
    public function genderToNum($gender)
    {
        switch ($gender) {
            case 'male':
                $code = 1;
                break;
            case 'female':
                $code = 2;
                break;
            default:
                $code = 0;
                break;
        }
        return $code;
    }
}